package service

import (
	"Wechat/dao"
	"Wechat/domain"
	"reflect"
	"time"
)

// SourceService 新建一个SourceService用于调用函数
type SourceService string

// 获取Mapper中的函数
var sourcemapper dao.SourceMapper

// AddSourceService 添加资源
func (s SourceService) AddSourceService(source domain.Source) bool {
	source.Time = time.Now()
	// 更新ID
	newSource := sourcemapper.GetNewSource()
	if reflect.DeepEqual(newSource, domain.Source{}) {
		source.ID = 1
	} else {
		source.ID = newSource.ID + 1
	}
	if sourcemapper.AddSource(source) > 0 {
		return true
	} else {
		return false
	}
}

// GetSourceCatagory 根据类别查看资源列表
func (s SourceService) GetSourceCategory(category string, id int) (bool, []domain.Source) {
	if id == 1 {
		id = 0
	}
	sources := sourcemapper.GetSourceCategory(category, id)
	if len(sources) == 0 {
		return false, sources
	} else {
		return true, sources
	}
}

// GetSourceList 返回所有资源
func (s SourceService) GetSourceList(id int) (bool, []domain.Source) {
	if id == 1 {
		id = 0
	}
	sources := sourcemapper.GetSource(id)
	if len(sources) == 0 {
		return false, sources
	} else {
		return true, sources
	}
}

// GetSourceDetail 返回资源详情
func (s SourceService) GetSourceDetail(id int) (bool, domain.Source) {
	if id <= 0 {
		return false, domain.Source{}
	}
	source := sourcemapper.GetDetail(id)
	//如果找到的结果为空
	if reflect.DeepEqual(source, domain.Source{}) {
		return false, domain.Source{}
	} else {
		return true, source
	}
}
